package cn.fl.fgi.controller;

import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.logger.eum.LogExceptionEnum;
import cn.cmm.base.framework.logger.eum.LogOperEnum;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.fl.fgi.api.dto.FgiBusStatusWebDTO;
import cn.fl.fgi.api.dto.FgiTranInfoDTO;
import cn.fl.fgi.service.IFgiBusStatusService;
import cn.fl.fgi.service.IFgiTranInfoService;
import cn.fl.framework.base.context.CurrentThreadContext;
import com.alibaba.fastjson.JSON;
import fl.constant.facade.framework.constant.BusinessIdConstant;
import fl.spl.facade.framework.constant.SplSystemConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @description 交易流水 控制层
 * @version V1.0
 */
@Controller
@RequestMapping("/fgi/fgitraninfo")
public class FgiTranInfoController extends BaseController {

    @Autowired
    private IFgiTranInfoService fgiTranInfoService;

    /**
     * 初始化列表
     */
    @RequestMapping(value = "/initTranInfoList.do", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object initTranInfoList(HttpServletRequest request, HttpServletResponse response) {
        BaseLogUtils.newWebLogger("初始化列表").setLogOperType(LogOperEnum.REQ)
                .setSysCd(CurrentThreadContext.getCurrentSysCd())
                .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                .setWarning(false)
                .info();
        String currentPage = request.getParameter("page");
        String pageSize = request.getParameter("pagesize");
        String busSourceType = request.getParameter("busSourceType");
        String busSource = request.getParameter("busSource");
        String busSourceId = request.getParameter("busSourceId");

        Pager page = new Pager();

        BaseLogUtils.newWebLogger("初始化列表").setLogOperType(LogOperEnum.REQ)
                .setSysCd(CurrentThreadContext.getCurrentSysCd())
                .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                .setWarning(false)
                .setKeys("page", JSON.toJSONString(page))
                .info();
        try {
            ListPageVO<FgiTranInfoDTO> listPage = null;
            List<FgiTranInfoDTO> list = this.fgiTranInfoService.selectFgiTranInfoList(busSourceId, busSource, busSourceType);

            if (CollectionUtils.isNotEmpty(list)) {
                listPage = new ListPageVO<>(list, list.size());
            } else {
                listPage = new ListPageVO<>(new ArrayList<>(), 0);
            }

            BaseLogUtils.newWebLogger("加载列表页面").setLogOperType(LogOperEnum.RESP)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .setKeys("listPage", JSON.toJSONString(listPage))
                    .info();
            return listPage;
        } catch (BaseException ex) {
            BaseLogUtils.newWebLogger("加载列表页面失败,业务异常").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .setThrowable(ex)
                    .warn(LogExceptionEnum.COMMON_CODE_0002);
        } catch (Exception ex) {
            BaseLogUtils.newWebLogger("加载列表页面失败,系统异常").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .setThrowable(ex)
                    .error(LogExceptionEnum.COMMON_CODE_0099);
        }
        return null;
    }

}

